package com.kgc.basicprovider.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kgc.basic.pojo.GoodsCategory;
import com.kgc.basic.util.PageInfo;
import com.kgc.basic.util.ResponseMsg;
import com.kgc.basicprovider.dao.GoodsCategoryMapper;
import com.kgc.basicprovider.service.GoodsCategoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class GoodsCategoryServiceImpl implements GoodsCategoryService{

    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = Exception.class)
    public ResponseMsg getGoodsCategoryList(Map<String, Object> paramMap) {
        ResponseMsg msg = new ResponseMsg();
        logger.info("GoodsCategoryServiceImpl getGoodsCategoryList paramMap:"+paramMap);
        PageInfo<GoodsCategory> pageInfo = new PageInfo<>();
        if(paramMap.containsKey("pageNo")){
            pageInfo.setPageNo(Integer.valueOf(paramMap.get("pageNo").toString()));
        }
        if(paramMap.containsKey("pageSize")){
            pageInfo.setPageSize(Integer.valueOf(paramMap.get("pageSize").toString()));
        }
        Page<Object> page = PageHelper.startPage(pageInfo.getPageNo(), pageInfo.getPageSize());
        List<GoodsCategory> goodsCategoryList = goodsCategoryMapper.getGoodsCategoryList(paramMap);
        logger.debug("GoodsCategoryServiceImpl getGoodsCategoryList goodsCategoryList:"+goodsCategoryList);
        pageInfo.setPages(page.getPages());
        pageInfo.setTotal((int)page.getTotal());
        pageInfo.setList(goodsCategoryList);
        logger.debug("GoodsCategoryServiceImpl getGoodsCategoryList pageInfo:"+pageInfo);
        msg.setCode("200");
        msg.setData(pageInfo);
        msg.setMsg("页面查询成功");
        return msg;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = Exception.class)
    public ResponseMsg getGoodsCategoryById(Integer id) {
        ResponseMsg msg = new ResponseMsg();
        logger.info("GoodsCategoryServiceImpl getGoodsCategoryById id:"+id);
        GoodsCategory goodsCategory = goodsCategoryMapper.getGoodsCategoryById(id);
        logger.debug("GoodsCategoryServiceImpl getGoodsCategoryById goodsCategory:"+goodsCategory);
        if(goodsCategory==null){
            msg.setCode("201");
            msg.setMsg("没找到商品分类");
            msg.setData(goodsCategory);
        }else{
            msg.setCode("200");
            msg.setMsg("已找到商品分类");
            msg.setData(goodsCategory);
        }
        logger.debug("GoodsCategoryServiceImpl getGoodsCategoryById msg:"+msg);
        return msg;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = Exception.class)
    public ResponseMsg getGoodsCategoryByName(String name) {
        ResponseMsg msg = new ResponseMsg();
        logger.info("GoodsCategoryServiceImpl getGoodsCategoryByName name:"+name);
        GoodsCategory goodsCategory = goodsCategoryMapper.getGoodsCategoryByName(name);
        logger.debug("GoodsCategoryServiceImpl getGoodsCategoryByName goodsCategory:"+goodsCategory);
        if(goodsCategory==null){
            msg.setCode("201");
            msg.setMsg("没找到商品分类");
            msg.setData(goodsCategory);
        }else{
            msg.setCode("200");
            msg.setMsg("已找到商品分类");
            msg.setData(goodsCategory);
        }
        logger.debug("GoodsCategoryServiceImpl getGoodsCategoryByName msg:"+msg);
        return msg;
    }

    @Override
    public ResponseMsg addGoodsCategory(GoodsCategory goodsCategory) {
        ResponseMsg msg = new ResponseMsg();
        goodsCategory.setCreateDate(new Date());
        goodsCategory.setCreateUserId(1);
        goodsCategory.setUpdateUserId(1);
        goodsCategory.setUpdateDate(new Date());
        logger.info("GoodsCategoryServiceImpl addGoodsCategory unit:"+goodsCategory);
        int insertCount = goodsCategoryMapper.insertGoodsCategory(goodsCategory);
        logger.debug("GoodsCategoryServiceImpl addGoodsCategory insertCount:"+insertCount);
        if(insertCount>0){
            msg.setCode("200");
            msg.setMsg("添加成功");
            msg.setData(true);
        }else{
            msg.setCode("201");
            msg.setMsg("添加失败");
            msg.setData(false);
        }
        logger.debug("GoodsCategoryServiceImpl addGoodsCategory msg:"+msg);
        return msg;
    }

    @Override
    public ResponseMsg modifyGoodsCategory(GoodsCategory goodsCategory) {
        ResponseMsg msg = new ResponseMsg();
        goodsCategory.setCreateUserId(1);
        goodsCategory.setUpdateUserId(1);
        goodsCategory.setUpdateDate(new Date());
        logger.info("GoodsCategoryServiceImpl modifyGoodsCategory goodsCategory:"+goodsCategory);
        int updateCount = goodsCategoryMapper.updateGoodsCategory(goodsCategory);
        logger.debug("GoodsCategoryServiceImpl modifyGoodsCategory updateCount:"+updateCount);
        if(updateCount>0){
            msg.setCode("200");
            msg.setMsg("修改成功");
            msg.setData(true);
        }else{
            msg.setCode("201");
            msg.setMsg("修改失败");
            msg.setData(false);
        }
        logger.debug("GoodsCategoryServiceImpl modifyGoodsCategory msg:"+msg);
        return msg;
    }

    @Override
    public ResponseMsg delGoodsCategory(Integer id) {
        ResponseMsg msg = new ResponseMsg();
        logger.info("GoodsCategoryServiceImpl delGoodsCategory id:"+id);
        int delCount = goodsCategoryMapper.delGoodsCategory(id);
        logger.debug("GoodsCategoryServiceImpl delGoodsCategory delCount:"+delCount);
        if(delCount>0){
            msg.setCode("200");
            msg.setMsg("删除成功");
            msg.setData(true);
        }else{
            msg.setCode("201");
            msg.setMsg("删除失败");
            msg.setData(false);
        }
        logger.debug("GoodsCategoryServiceImpl delGoodsCategory msg:"+msg);
        return msg;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,isolation = Isolation.DEFAULT,rollbackFor = Exception.class)
    public ResponseMsg showGoodsCategoryByTree() {
        logger.info("GoodsCategoryServiceImpl showGoodsCategoryByTree is start");
        ResponseMsg msg = new ResponseMsg();
        //查询所有的商品分类信息
        List<GoodsCategory> goodsCategorieList = goodsCategoryMapper.showGoodsCategoryList();
        logger.debug("GoodsCategoryServiceImpl showGoodsCategoryByTree goodsCategorieList:"+goodsCategorieList);
        //用来存储树形商品分类信息
        List<GoodsCategory> goodsCategoryTree = new ArrayList<>();
        //根据对应商品分类id存储对应商品分类信息
        Map<Integer,GoodsCategory> goodsCategoryMap = new HashMap<>();

        //根据对应商品分类id存储对应商品分类信息
        for(GoodsCategory goodsCategory:goodsCategorieList){
            //防止空指针
            goodsCategory.setChildren(new ArrayList<>());
            goodsCategoryMap.put(goodsCategory.getId(),goodsCategory);
        }
        logger.debug("GoodsCategoryServiceImpl showGoodsCategoryByTree goodsCategoryMap:"+goodsCategoryMap);
        //进行树形拼接
        for(GoodsCategory goodsCategory:goodsCategorieList) {

            if(goodsCategory.getParentId()==0){
                goodsCategoryTree.add(goodsCategory);
                continue;
            }
            GoodsCategory goodsParentCategory = goodsCategoryMap.get(goodsCategory.getParentId());
            goodsParentCategory.getChildren().add(goodsCategory);
        }
        logger.debug("GoodsCategoryServiceImpl showGoodsCategoryByTree goodsCategoryTree:"+goodsCategoryTree);
        msg.setCode("200");
        msg.setData(goodsCategoryTree);
        return msg;

        /**
         * @Override
         * public ResponseMsg selectGoodsCategoryTree() {
         *     logger.info("GoodsCategoryServiceImpl.selectGoodsCategoryTree");
         *     ResponseMsg responseMsg = new ResponseMsg();
         *     List<GoodsCategory> goodsCategoryList = goodsCategoryMapper.selectAllGoodsCategory();
         *     Map<Integer,GoodsCategory> goodsCategoryMap = new HashMap<>();
         *     for (GoodsCategory goodsCategory: goodsCategoryList){
         *         goodsCategoryMap.put(goodsCategory.getId(),goodsCategory);
         *     }
         *     //Map<Integer,GoodsCategory> goodsCategoryMap=goodsCategoryList.stream()
         *     // .collect(Collectors.toMap(GoodsCategory::getId,goodsCategory -> goodsCategory));
         *     List<GoodsCategory> goodsCategoryTree = new ArrayList<>();
         *     for (GoodsCategory goodsCategory:goodsCategoryList){
         *         goodsCategory.setChildren(new ArrayList<>());
         *         if(goodsCategory.getParentId()==0) {
         *             goodsCategoryTree.add(goodsCategory);
         *             continue;
         *         }
         *         GoodsCategory parentGoodsCategory = goodsCategoryMap.get(goodsCategory.getParentId());
         *         parentGoodsCategory.getChildren().add(goodsCategory);
         *     }
         *     responseMsg.setCode("200");
         *     responseMsg.setData(goodsCategoryTree);
         *     logger.debug("GoodsCategoryServiceImpl.selectGoodsCategoryTree" + " responseMsg = " + responseMsg);
         *     return responseMsg;
         * }
         */
    }

    @Override
    public ResponseMsg getGoodsCategoryCode(String code) {
        ResponseMsg msg = new ResponseMsg();
        logger.info("GoodsCategoryServiceImpl getGoodsCategoryCode code:"+code);
        GoodsCategory goodsCategory = goodsCategoryMapper.getGoodsCategoryByCode(code);
        logger.debug("GoodsCategoryServiceImpl getGoodsCategoryCode goodsCategory:"+goodsCategory);
        if(goodsCategory==null){
            msg.setCode("201");
            msg.setMsg("没找到商品分类");
            msg.setData(goodsCategory);
        }else{
            msg.setCode("200");
            msg.setMsg("已找到商品分类");
            msg.setData(goodsCategory);
        }
        logger.debug("GoodsCategoryServiceImpl getGoodsCategoryCode msg:"+msg);
        return msg;
    }

    @Override
    public ResponseMsg getGoodsCategoryByParentId(Integer parentId) {
        ResponseMsg msg = new ResponseMsg();
        logger.info("GoodsCategoryServiceImpl getGoodsCategoryByParentId parentId:"+parentId);
        List<GoodsCategory> goodsCategoryList = goodsCategoryMapper.getGoodsCategoryByParentId(parentId);
        logger.debug("GoodsCategoryServiceImpl getGoodsCategoryByParentId goodsCategoryList:"+goodsCategoryList);
        if(goodsCategoryList==null||goodsCategoryList.isEmpty()){
            msg.setCode("201");
            msg.setMsg("没找到子分类");
            msg.setData(goodsCategoryList);
        }else{
            msg.setCode("200");
            msg.setMsg("已找到子商品分类");
            msg.setData(goodsCategoryList);
        }
        logger.debug("GoodsCategoryServiceImpl getGoodsCategoryByParentId msg:"+msg);
        return msg;
    }
}
